package com.ophiux.utils.common;

import java.util.Calendar;

/**
 * 说明：身份证工具类<br>
 * <h1>创 建 人: hehailong<br>
 * </h1> 创建日期: 2018年10月17日 上午9:18:00<br>
 * 修改记录：
 */
public class IDCardUtil {

	/**
	 * 获取岁数
	 * 
	 * @param idCard 身份证
	 * @return String age
	 */
	public static String getAgeByIdCard(String idCard) {
		return getAge(idCard) + "";
	}

	/**
	 * 获取岁数
	 * 
	 * @param idCard 身份证
	 * @return int age
	 */
	public static int getAgeByIdCard2(String idCard) {
		return getAge(idCard);
	}

	public static String getSex(String idCard) {
		String sex = "未知";
		if (StringUtil.isEmpty(idCard)) {
			return "";
		}
		if (idCard.length() == 15) {
			idCard = fixPersonIDCode(idCard);
		}
		int num = Integer.parseInt(idCard.substring(idCard.length() - 2, idCard.length() - 1));
		if (num % 2 == 0) {
			sex = "女";
		} else {
			sex = "男";
		}
		return sex;
	}

	public static int getAge(String idCard) {
		if (StringUtil.isEmpty(idCard)) {
			return 0;
		}
		String birthTimeString = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-"
				+ idCard.substring(12, 14);

		// 先截取到字符串中的年、月、日
		String strs[] = birthTimeString.trim().split("-");
		int selectYear = Integer.parseInt(strs[0]);
		int selectMonth = Integer.parseInt(strs[1]);
		int selectDay = Integer.parseInt(strs[2]);
		// 得到当前时间的年、月、日
		Calendar cal = Calendar.getInstance();
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayNow = cal.get(Calendar.DATE);

		// 用当前年月日减去生日年月日
		int yearMinus = yearNow - selectYear - 1;
		int monthMinus = monthNow - selectMonth;
		int dayMinus = dayNow - selectDay;

		int age = yearMinus;
		if (yearMinus < 0) {// 选了未来的年份
			age = 0;
		} else if (yearMinus == 0) {// 同年的，要么为1，要么为0
			if (monthMinus < 0) {// 选了未来的月份
				age = 0;
			} else if (monthMinus == 0) {// 同月份的
				if (dayMinus < 0) {// 选了未来的日期
					age = 0;
				} else if (dayMinus >= 0) {
					age = 1;
				}
			} else if (monthMinus > 0) {
				age = 1;
			}
		} else if (yearMinus > 0) {
			if (monthMinus < 0) {// 当前月>生日月
			} else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
				if (dayMinus < 0) {
				} else if (dayMinus >= 0) {
					age = age + 1;
				}
			} else if (monthMinus > 0) {
				age = age + 1;
			}
		}
		return age;
	}

	/**
	 * 
	 * 修补15位居民身份证号码为18位
	 * 
	 * @param personIDCode
	 * @return
	 */
	public static String fixPersonIDCode(String personIDCode) {
		String retIDCode = "";

		if (personIDCode == null || personIDCode.trim().length() != 15) {
			return personIDCode;
		}
		String id17 = personIDCode.substring(0, 6) + "19" + personIDCode.substring(6, 15); // 15为身份证补\'19\'
		char[] code = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // 11个
		int[] factor = { 0, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 }; // 18个;

		int[] idcd = new int[18];
		int i;
		int j;
		int sum;
		int remainder;

		for (i = 1; i < 18; i++) {
			j = 17 - i;// 16
			idcd[i] = Integer.parseInt(id17.substring(j, j + 1));
		}

		sum = 0;
		for (i = 1; i < 18; i++) {
			sum = sum + idcd[i] * factor[i];
		}

		remainder = sum % 11;
		String lastCheckBit = String.valueOf(code[remainder]);
		return id17 + lastCheckBit;

	}

	/**
	 * 
	 * @param idCard
	 * @param splitChar 最后年龄格式的拼接符
	 * @return
	 */
	public static String getBirthday(String idCard, String splitChar) {
		if (StringUtil.isEmpty(idCard)) {
			return "";
		}
		if (idCard.length() == 15) {
			idCard = fixPersonIDCode(idCard);
		}

		String birthday = "";
		int selectYear = Integer.parseInt(idCard.substring(6, 10));
		int selectMonth = Integer.parseInt(idCard.substring(10, 12));
		int selectDay = Integer.parseInt(idCard.substring(12, 14));

		birthday = selectYear + splitChar + selectMonth + splitChar + selectDay;

		return birthday;
	}

	public static void main(String[] args) {
		String age = null;
		age = IDCardUtil.getSex("511322199510103373");
		System.out.println(age);
	}

}
